package com.example.demo.Service.impl;

import com.example.demo.Common.Category;
import com.example.demo.Context.BaseContext;
import com.example.demo.Mapper.CategoryMapper;
import com.example.demo.Proj.CategoryPageParam;
import com.example.demo.Proj.PageReslut;
import com.example.demo.Proj.StatusConstant;
import com.example.demo.Service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageReslut getCategoryPage(CategoryPageParam categoryPageParam) {
        PageHelper.startPage(categoryPageParam.getPage(), categoryPageParam.getPageSize());
        Page<Category> categoryPage = categoryMapper.getCategoryPage(categoryPageParam);
        return new PageReslut(categoryPage.getTotal(), categoryPage.getResult());
    }

    @Override
    public void addCategory(Category category) {
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.addCategory(category);
    }

    @Override
    public void editCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public void deleCategory(Long id) {
        categoryMapper.deleCategory(id);
    }

    @Override
    public void enableOrDisableEmployee(Long id, Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }
}
